/*
Copyright (c) [2019年5月1日] [吴超]
[MBT_studio] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
		 http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/
#ifndef INCLUDE_SHADER_SAMPLE_CURVE
#define INCLUDE_SHADER_SAMPLE_CURVE

#include "shader_curve.h"



#ifdef __cplusplus

#include <向量.h>

// GLSL Type
using uint = unsigned int;
#define in
#define out
#define inout



#else

R_GlobalParameter_CurevPoint curvePoint_GParam = R_GlobalParameter_CurevPoint(gGlobalParam.m_AddressSampleCurve);



float f_glsl_CurveSample(float samplePos, in uvec2 curve, in vec2 cl) {
	if (curve.y < 2) return 0;

	uint index = curve.x;
	S_CurvePoint pointA = f_curve_cal_pre(curvePoint_GParam.curvePointPtr[index], curvePoint_GParam.curvePointPtr[index + 1]);
	
	samplePos = clamp(samplePos, cl.x, cl.y);
	

	for (uint i = 0; i < curve.y; ++i) {
		S_CurvePoint pointB = curvePoint_GParam.curvePointPtr[index + i];

		if (samplePos < pointB.value.x) {
			float steps = samplePos - pointA.value.x;
			float gap = (pointB.value.x - pointA.value.x);
			steps = steps / (gap + 0.000001);

			vec2 value = f_curve_cal_point(pointA, pointB, steps);
			return clamp(value.y, cl.x, cl.y);
		}

		pointA = pointB;
	}

	vec2 value = f_curve_cal_point(pointA, f_curve_cal_next(pointA), samplePos);
	return clamp(value.y, cl.x, cl.y);
}



#endif







#endif //TRANSFORM

